<template>
	<view class="page-container">
		<!-- 页面标题 -->
		<view class="page-header">
			<view class="header-content">
				<view class="header-icon">🔍</view>
				<view class="header-text">
					<text class="header-title">选择学生</text>
					<text class="header-subtitle">搜索并绑定学生信息</text>
				</view>
			</view>
		</view>

		<!-- 搜索区域 -->
		<view class="search-container">
			<up-search
				placeholder="请输入学生姓名"
				v-model="fromData.name"
				@search="handlerQuery"
				@clear="handlerQuery"
				bgColor="#F8FAFB"
				borderColor="transparent"
				searchIconColor="#6FD719"
				height="80"
				margin="0 30rpx"
			></up-search>
		</view>

		<!-- 内容区域 -->
		<view class="content-container">
			<!-- 加载状态 -->
			<template v-if="loading">
				<view class="loading-container">
					<up-loading-icon mode="spinner" size="40" color="#6FD719"></up-loading-icon>
					<text class="loading-text">正在搜索学生...</text>
				</view>
			</template>

			<!-- 学生列表 -->
			<template v-else-if="dataList.length > 0">
				<view class="student-list">
					<view class="list-header">
						<up-icon name="list" color="#6FD719" size="16"></up-icon>
						<text class="list-title">搜索结果 ({{dataList.length}})</text>
					</view>

					<view class="student-item" v-for="(item, index) in dataList" :key="index" @click="handlerBind(item)">
						<view class="student-avatar">
							<text class="avatar-text">{{item.name.charAt(0)}}</text>
						</view>
						<view class="student-info">
							<text class="student-name">{{item.name}}</text>
							<view class="student-details">
								<view class="detail-item" v-if="item.studentId">
									<up-icon name="bookmark" color="#999" size="12"></up-icon>
									<text class="detail-text">学号: {{item.studentId}}</text>
								</view>
								<view class="detail-item" v-if="item.className">
									<up-icon name="grid" color="#999" size="12"></up-icon>
									<text class="detail-text">班级: {{item.className}}</text>
								</view>
							</view>
						</view>
						<view class="bind-action">
							<up-icon name="plus-circle" color="#6FD719" size="20"></up-icon>
							<text class="bind-text">绑定</text>
						</view>
					</view>
				</view>
			</template>

			<!-- 空状态 -->
			<template v-else>
				<view class="empty-container">
					<up-empty
						mode="search"
						text="未找到相关学生"
						textColor="#999"
						iconColor="#ddd"
					></up-empty>
					<view class="empty-tip">
						<text>请尝试输入完整的学生姓名</text>
					</view>
				</view>
			</template>
		</view>
	</view>
</template>

<script setup>
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		reactive, ref
	} from 'vue';
	import {
		selectStudent,createStudent
	} from '../../api/student.js'

	const fromData = ref({
		schoolId: "",
		gradeId: "",
		classesId: "",
		name: ""
	})
	const dataList = ref([])
	const loading = ref(false)

	onLoad((options) => {
		// 解码参数
		fromData.value.schoolId = decodeURIComponent(options.schoolId || '');
		fromData.value.gradeId = decodeURIComponent(options.gradeId || '');
		fromData.value.classesId = decodeURIComponent(options.classesId || '');
		// 页面加载完成后自动查询
		queryData();
	})
	
	const queryData = async () => {
		try {
			loading.value = true;
			dataList.value = await selectStudent(fromData.value);
		} catch (error) {
			console.error('查询失败:', error);
			uni.showToast({
				title: '查询失败，请重试',
				icon: 'none'
			});
		} finally {
			loading.value = false;
		}
	}
	
	function handlerQuery(){
		queryData();
	}
	
	const handlerBind = async (item) => {
		uni.showModal({
			title: '确认绑定',
			content: `确认绑定学生${item.name}?`,
			async success(res) {
				if (res.confirm) {
					try {
						// 检查登录状态
						const userInfo = uni.getStorageSync("userInfo")
						if (!userInfo || !userInfo.id) {
							uni.showToast({
								title: '请先登录',
								icon: 'none'
							});
							return;
						}
						
						// 显示loading
						uni.showLoading({
							title: '绑定中...',
							mask: true
						});
						
						item.parentId = userInfo.id;
						await createStudent(item);
						
						uni.hideLoading();
						uni.showToast({
							title: '绑定成功',
							icon: 'success',
							duration: 2000
						});
						
						setTimeout(() => {
							uni.navigateBack({
								delta: 2
							});
						}, 1500);
					} catch (error) {
						uni.hideLoading();
						// 根据错误类型显示不同的错误信息
						const errorMsg = error.message || '绑定失败，请重试';
						uni.showToast({
							title: errorMsg,
							icon: 'none',
							duration: 3000
						});
						console.error('绑定学生失败:', error);
					}
				}
			}
		});
	}
</script>

<style lang="scss" scoped>
	.page-container {
		background: linear-gradient(180deg, #F8FAFB 0%, #FFFFFF 100%);
		min-height: 100vh;
		padding-bottom: 40rpx;
	}

	/* 页面标题 */
	.page-header {
		background: linear-gradient(135deg, #6FD719 0%, #5BC016 100%);
		padding: 60rpx 30rpx 40rpx;
		border-radius: 0 0 40rpx 40rpx;
		box-shadow: 0 4px 20px rgba(111, 215, 25, 0.2);
		position: relative;
		overflow: hidden;
	}

	.page-header::before {
		content: '';
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: linear-gradient(45deg, rgba(255, 255, 255, 0.1) 0%, transparent 100%);
		pointer-events: none;
	}

	.header-content {
		display: flex;
		align-items: center;
		gap: 20rpx;
	}

	.header-icon {
		font-size: 48rpx;
		width: 80rpx;
		height: 80rpx;
		background: rgba(255, 255, 255, 0.2);
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.header-text {
		flex: 1;
	}

	.header-title {
		display: block;
		font-size: 36rpx;
		font-weight: 600;
		color: #FFFFFF;
		margin-bottom: 8rpx;
	}

	.header-subtitle {
		font-size: 26rpx;
		color: rgba(255, 255, 255, 0.8);
	}

	/* 搜索区域 */
	.search-container {
		padding: 30rpx 0;
	}

	/* 内容区域 */
	.content-container {
		padding: 0 30rpx;
	}

	/* 加载状态 */
	.loading-container {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		padding: 80rpx 20rpx;
		background: linear-gradient(135deg, #FFFFFF 0%, #FAFBFC 100%);
		border-radius: 25rpx;
		box-shadow: 0 4px 16px rgba(0, 0, 0, 0.04);
	}

	.loading-text {
		font-size: 28rpx;
		color: #666;
		margin-top: 24rpx;
	}

	/* 学生列表 */
	.student-list {
		background: linear-gradient(135deg, #FFFFFF 0%, #FAFBFC 100%);
		border-radius: 25rpx;
		box-shadow: 0 4px 16px rgba(0, 0, 0, 0.04);
		border: 1px solid rgba(255, 255, 255, 0.8);
		overflow: hidden;
	}

	.list-header {
		display: flex;
		align-items: center;
		gap: 12rpx;
		padding: 30rpx;
		border-bottom: 2rpx solid #F5F7FA;
		background: rgba(111, 215, 25, 0.03);
	}

	.list-title {
		font-size: 30rpx;
		font-weight: 600;
		color: #333;
	}

	.student-item {
		display: flex;
		align-items: center;
		padding: 30rpx;
		border-bottom: 1px solid #F5F7FA;
		transition: all 0.3s ease;

		&:last-child {
			border-bottom: none;
		}

		&:active {
			background: rgba(111, 215, 25, 0.05);
			transform: scale(0.98);
		}
	}

	.student-avatar {
		width: 80rpx;
		height: 80rpx;
		background: linear-gradient(135deg, #6FD719 0%, #5BC016 100%);
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-right: 20rpx;
		box-shadow: 0 4px 12px rgba(111, 215, 25, 0.3);
	}

	.avatar-text {
		font-size: 32rpx;
		font-weight: 600;
		color: #FFFFFF;
	}

	.student-info {
		flex: 1;
	}

	.student-name {
		font-size: 32rpx;
		font-weight: 600;
		color: #333;
		margin-bottom: 8rpx;
		display: block;
	}

	.student-details {
		display: flex;
		flex-direction: column;
		gap: 8rpx;
	}

	.detail-item {
		display: flex;
		align-items: center;
		gap: 8rpx;
	}

	.detail-text {
		font-size: 24rpx;
		color: #666;
	}

	.bind-action {
		display: flex;
		flex-direction: column;
		align-items: center;
		gap: 8rpx;
		padding: 10rpx;
		border-radius: 15rpx;
		background: rgba(111, 215, 25, 0.1);
		border: 1px solid rgba(111, 215, 25, 0.2);
	}

	.bind-text {
		font-size: 24rpx;
		color: #6FD719;
		font-weight: 500;
	}

	/* 空状态 */
	.empty-container {
		background: linear-gradient(135deg, #FFFFFF 0%, #FAFBFC 100%);
		border-radius: 25rpx;
		padding: 80rpx 30rpx;
		text-align: center;
		box-shadow: 0 4px 16px rgba(0, 0, 0, 0.04);
	}

	.empty-tip {
		margin-top: 20rpx;

		text {
			font-size: 26rpx;
			color: #999;
		}
	}

	/* 动画效果 */
	.student-item {
		animation: slideInUp 0.4s ease-out;
	}

	@keyframes slideInUp {
		from {
			opacity: 0;
			transform: translateY(20rpx);
		}
		to {
			opacity: 1;
			transform: translateY(0);
		}
	}
</style>